package com.epi.basic.handler;

import com.alibaba.fastjson.JSON;
import com.epi.basic.model.ErrorCode;
import com.epi.basic.model.ResponseVO;
import com.epi.utils.WebUtils;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author dxy
 * @version 1.0
 */
@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException e) throws IOException, ServletException {
        ResponseVO<Object> result = ResponseVO.from(ErrorCode.FORBIDDEN);
        WebUtils.renderString(response, JSON.toJSONString(result));
    }
}
